﻿if (typeof Phule == "undefined") Phule = {};
if (typeof Phule.Web == "undefined") Phule.Web = {};
Phule.Web.productchecking = {
    msgSuccessChecking: '',
    msgUnSuccessChecking: '',
    availableTags: [],
    availableDataSearch: [],
    Init: function () {
        var objThis = this;
        this.AutoComplete();
        if (typeof Site_Message != "undefined")
            $.each(Site_Message, function (index, item) {
                if (item.Name == 'CheckingSuccessfull')
                    objThis.msgSuccessChecking = item.Description;
                if (item.Name == 'CheckingUnSuccessfull')
                    objThis.msgUnSuccessChecking = item.Description;
            });
    },
    AutoComplete: function () {
        var objThis = this;
        $("#txtSearchCode").autocomplete({
            source: function (request, response) {
                $.ajax(
                {
                    url: '../services/ProductService.asmx/CheckCode',
                    dataType: "json",
                    type: 'POST',
                    data:
                    {
                        pCode: request.term,
                    },
                    beforeSend: function () {
                        $(".wait").show();
                        $("#ResultTable").hide();
                        $("#ResultChecking").html('&nbsp;');
                    },
                    success: function (pData) {
                        //console.log('availableTags', pData);
                        objThis.availableTags = new Array();
                        objThis.availableDataSearch = pData;
                        //var data = eval("(" + pData.d + ")");
                        $.each(pData, function (index, item) {
                            objThis.availableTags.push(item.Code);
                        });
                        // console.log('availableTags', availableTags);
                        response(objThis.availableTags);
                    }
                });
            },
            search: function (event, ui) {
                $(".wait").show();
            },
            open: function (event, ui) {
                $(".wait").hide();
            },
            close: function (event, ui) {

                // console.log('close',event, ui);

            },
            change: function (event, ui) {
                //  console.log('change', event, ui);
            },
            select: function (event, ui) {
                window.setTimeout(function () {
                    objThis.ViewResult();
                }, 1000);
            }
        });
        $("#txtSearchCode").blur(function () {
            objThis.ViewResult();
        });
    },
    ViewResult: function () {
        var objThis = this;
        var inputValue = $("#txtSearchCode").val();
        $(".wait").hide();
        $("#ResultTable").hide();
        var idx = jQuery.inArray(inputValue, objThis.availableTags);
        if (inputValue == '') {
            $("#ResultChecking").html('&nbsp;');
            return;
        }
        if (idx == -1) {
            $("#ResultChecking").html(objThis.msgUnSuccessChecking);
        }
        else {
            $("#ResultChecking").html(objThis.msgSuccessChecking);
            $.each(objThis.availableDataSearch, function (index, item) {
                if (item.Code == inputValue) {
                    $("#tdBrand").html(item.Description);
                    $("#tdCode").html(item.Code);
                    $("#tdDate").html(objThis.ParseDate(item.ReleaseDate));
                }
            });
            $("#ResultTable").show();
        }
    },
    ParseDate: function (src) {
        try {
            src = src.replace(/[^0-9 +]/g, '');
            var myDate = new Date(parseInt(src));
            var yyyy = myDate.getFullYear();
            var dd = myDate.getDate();
            var MM = myDate.getMonth() + 1;
            if (MM < 10)
                MM = "0" + MM;
            if (dd < 10)
                dd = "0" + dd;
            return dd + "/" + MM + "/" + yyyy;
        }
        catch (ex) { }
        return "&nbsp;";
    }
}
$(function () {
    Phule.Web.productchecking.Init();
});